﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DocList"/>.
    /// </summary>
    public class DocListVM : DocCommentVM
    {
        #region /* STATICS */

        internal static new void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(DocList),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new DocListVM((DocList)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DocList"/>.
        /// </summary>
        public DocListVM(DocList docList, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(docList, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DocList"/> model.
        /// </summary>
        public DocList DocList
        {
            get { return (DocList)CodeObject; }
        }

        #endregion

        #region /* RENDERING */

        protected override void RenderStart(CodeRenderer renderer, RenderFlags flags)
        {
            if (!flags.HasFlag(RenderFlags.Description))
            {
                Brush brush = (DocList.MissingEndTag ? WARNING_BRUSH : COMMENT_TAG_BRUSH);
                if (!UseAlternativeFormat)
                {
                    renderer.RenderText("<" + DocList.TagName, brush, this);
                    renderer.RenderText(" " + DocList.AttributeName + "=\"" + DocList.Type + "\"", COMMENT_TAG_BRUSH, this);
                    renderer.RenderText(_contentVM == null && !DocList.MissingEndTag ? "/>" : ">", brush, this);
                }
                else
                {
                    TextStyle textStyle = (UseProportionalFont ? TextStyle.Proportional : TextStyle.Normal);
                    renderer.RenderText(char.ToUpper(DocList.TagName[0]) + DocList.TagName.Substring(1) + " :  ", brush, textStyle, this);
                    renderer.RenderText(DocList.Type, COMMENT_TAG_BRUSH, textStyle, this);
                }
            }
        }

        protected override void RenderContent(CodeRenderer renderer, TextStyle textStyle, RenderFlags flags)
        {
            if (_contentVM is string)
                DocTextVM.RenderText(renderer, GetContentForDisplay(flags | (UseAlternativeFormat ? RenderFlags.NoTagNewLines : 0)), textStyle | TextStyle.NoCache, flags, this);
            else if (_contentVM is ChildListVM<DocCommentVM>)
            {
                ChildListVM<DocCommentVM> content = (ChildListVM<DocCommentVM>)_contentVM;
                for (int i = 0; i < content.Count; ++i)
                {
                    DocCommentVM docComment = content[i];
                    if (docComment is DocTextVM)
                    {
                        string text = GetContentForDisplay((DocTextVM)docComment, i == 0, i == content.Count - 1, flags | (UseAlternativeFormat && i != 0 ? RenderFlags.NoTagNewLines : 0));
                        DocTextVM.RenderText(renderer, text, textStyle | TextStyle.NoCache, flags, this);
                    }
                    else
                        docComment.Render(renderer, textStyle, flags);
                }
            }
            else if (_contentVM is CodeObjectVM)
                ((CodeObjectVM)_contentVM).Render(renderer, flags);
        }

        #endregion
    }
}
